package com.anycl.customer.service.impl;

import com.anycl.customer.constant.Constants;
import com.anycl.customer.dao.CustomerRepository;
import com.anycl.customer.entity.Customer;
import com.anycl.customer.exception.CustomerException;
import com.anycl.customer.service.AdminUserService;
import com.anycl.customer.service.CustomerService;
import com.anycl.customer.vo.AdminUserVo;
import com.anycl.customer.vo.CustomerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CustomerServiceImpl implements CustomerService {

    private final AdminUserService adminUserService;

    private final CustomerRepository customerRepository;

    public CustomerServiceImpl(AdminUserService adminUserService, CustomerRepository customerRepository) {
        this.adminUserService = adminUserService;
        this.customerRepository = customerRepository;
    }


    @Override
    @Transactional
    public CustomerResponse createCustomer(Customer customer, HttpSession session) throws CustomerException {
        isLogin(session);
        if (!customer.createValidate())
            throw new CustomerException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        customer.setStatus(Constants.OK_STATUS);
        customer.setCreateTime(new Date());
        customer.setUpdateTime(new Date());
        Customer newCustomer = customerRepository.save(customer);
        return new CustomerResponse(newCustomer.getId(), newCustomer.getName());
    }

    @Override
    @Transactional
    public CustomerResponse removeCustomer(String id, HttpSession session) throws CustomerException {
        isLogin(session);
        Optional<Customer> customer = customerRepository.findById(id);
        if (!customer.isPresent())
            throw new CustomerException(Constants.ErrorMsg.CAN_NOT_FOUND_RECORD);
        customer.get().setStatus(Constants.NOT_OK_STATUS);
        customer.get().setUpdateTime(new Date());
        customerRepository.save(customer.get());
        return new CustomerResponse(customer.get().getId(), customer.get().getName());
    }

    @Override
    @Transactional
    public CustomerResponse updateCustomer(Customer customer, HttpSession session) throws CustomerException {
        isLogin(session);
        if (customer.getId() == null)
            throw new CustomerException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);

        Optional<Customer> dbCustomer = customerRepository.findById(customer.getId());
        if (!dbCustomer.isPresent())
            throw new CustomerException(Constants.ErrorMsg.CAN_NOT_FOUND_RECORD);
        if (customer.getName() != null)
            dbCustomer.get().setName(customer.getName());
        if (customer.getAddress() != null)
            dbCustomer.get().setAddress(customer.getAddress());
        if (customer.getTelephone() != null)
            dbCustomer.get().setTelephone(customer.getTelephone());
        if (customer.getEmail() != null)
            dbCustomer.get().setEmail(customer.getEmail());
        if (customer.getStatus() != null)
            dbCustomer.get().setStatus(customer.getStatus());
        if (customer.getRemark() != null)
            dbCustomer.get().setRemark(customer.getRemark());
        dbCustomer.get().setUpdateTime(new Date());
        customerRepository.save(dbCustomer.get());
        return new CustomerResponse(dbCustomer.get().getId(), dbCustomer.get().getName());
    }

    @Override
    public Customer getCustomer(String id, HttpSession session) throws CustomerException {
        isLogin(session);
        return customerRepository.findByIdAndStatus(id, Constants.OK_STATUS);
    }

    @Override
    public List<Customer> getCustomerList(HttpSession session) throws CustomerException {
        isLogin(session);
        return customerRepository.findAllByStatus(Constants.OK_STATUS);
    }

    private boolean isLogin(HttpSession session) throws CustomerException {
        AdminUserVo adminUserVo = adminUserService.isLogin(session);
        if (adminUserVo == null) {
            throw new CustomerException(Constants.ErrorMsg.USER_LOGIN_ERROR);
        }
        return true;
    }
}
